﻿// Serotonin -- easy-to-use image filters application
// https://code.google.com/p/serotonin/
//
// Copyright © Frank Nagl, 2012-2013
// admin@franknagl.de
//
namespace Serotonin.OptionsForms
{
    using Controls;
    using Filter;
    using Helper;
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;

    /// <summary>Filter specified properties form.</summary>
    public partial class ImageSketchManager : Form, IOptionsForm
    {
        /// <summary>
        /// Control to display an image for sketching figures in it.
        /// </summary>
        public ImageSketchControl ImageSketchControl { get; private set; }
        /// <summary>
        /// The processor for managing the images and the image GUI panel.
        /// </summary>
        public Processor Processor { get; set; }

        private readonly Bitmap origImageScaled;
        // Scale fcators for scaling image to panel size.         
        private readonly float scaleX;
        private readonly float scaleY;

        /// <summary>
        /// Initializes a new instance of the <see cref="ConGrapForm" /> class.
        /// </summary>
        /// <param name="processor">The processor.</param>
        /// <param name="panelW">The max W.</param>
        /// <param name="panelH">The max H.</param>
        public ImageSketchManager(Processor processor, int panelW, int panelH)
        {
            InitializeComponent();
            AOF.Init(processor, this, gbProcess, null);
            origImageScaled = BitmapHelper.CloneBitmap(Processor.OriginalImage, 
                panelW, panelH, Processor.OriginalImage.PixelFormat, true);

            ImageSketchControl = new ImageSketchControl(origImageScaled)
                                     {
                                         Width = panelW, 
                                         Height = panelH
                                     };

            int oiw = Processor.OriginalImage.Width;
            int oih = Processor.OriginalImage.Height;

            if (oiw > panelW || oih > panelH)
            {
                float ratio = oiw / (float)oih;
                int w, h;
                if (oiw > oih)
                {
                    w = panelW;
                    h = (int)(w / ratio + 0.5f);
                }
                else
                {
                    h = panelH;
                    w = (int)(h * ratio + 0.5f);
                }

                scaleX = oiw / (float)w;
                scaleY = oih / (float)h;
                // b = new Bitmap(b, w, h);
            }
            else
            {
                scaleX = 1;
                scaleY = 1;
            }

            // TODO: Loading imageSketchControl.Figures here ...

            //// inverse scaling to display correctly the figures
            //ScaleImageSketchControlFigures(1 / ScaleX, 1 / ScaleY);
        }

        /// <summary> Applies the specified filter in the options form. </summary>
        public void ApplyFilter()
        {
            // string needs to be created first...
            string s = Program.L.Text[40] + Program.L.Text[10] + ": " +
            ImageSketchControl.Figures.Count + " " +
            Program.L.Text[82] + "; ";

            ImageSketch filter = new ImageSketch(ScaleImageSketchControlFigures(
                ImageSketchControl.Figures, scaleX, scaleY));
            Bitmap b = filter.Apply(Processor.OriginalImage);
            // new SimpleImageViewer(b);

            //// scaling figures to correct size
            //ScaleImageSketchControlFigures(ScaleX, ScaleY);
            Processor.WorkingImage = b;

            b = BitmapHelper.CloneBitmap(b, origImageScaled.Width,
                origImageScaled.Height, b.PixelFormat, true);
            ImageSketchControl.ChangeImageAndResetFigures(b);

            Processor.Change(s);
        }

        /// <summary>
        /// Sets the language specified string texts for the form elements.
        /// </summary>
        public void SetStrings()
        {
            // Process groupbox
            gbProcess.Text = Program.L.Text[54];
            btnProcess.Text = Program.L.Text[55];
            btnReset.Text = Program.L.Text[56];

            Options.Text = Program.L.Text[10];
            tabctrlOptions.TabPages[0].Text = Program.L.Text[71];

            lbMouseLeft.Text = Program.L.Text[72];
            lbMouseMiddle.Text = Program.L.Text[73];
            lbMouseRight.Text = Program.L.Text[74];
            lbKeyCtrl.Text = Program.L.Text[75];
            lbKeyEsc.Text = Program.L.Text[76];
            lbMouseLeftDescr.Text = Program.L.Text[77];
            lbMouseMiddleDescr.Text = Program.L.Text[78];
            lbMouseRightDescr.Text = Program.L.Text[79];
            lbKeyCtrlDescr.Text = Program.L.Text[80];
            lbKeyEscDescr.Text = Program.L.Text[81];
        }

        private static List<Figure> ScaleImageSketchControlFigures(
            List<Figure> figures, float sX, float sY)
        {
            List<Figure> scaledFigs = new List<Figure>(figures.Count);
            scaledFigs.AddRange(figures);

            foreach (Figure f in scaledFigs)
            {
                f.Scale(sX, sY);
            }
            return scaledFigs;
        }

        private void BtnProcessClick(object sender, EventArgs e)
        {

            //EndlessProgressBarFormInThread form =
            //    new EndlessProgressBarFormInThread(
            //        "Do Contour Tracing ... ",
            //        "Please wait, this takes a few seconds.");
            //form.Start();

            try
            {                
                ApplyFilter();
            }
            catch (ArgumentException aex)
            {
                MessageBox.Show(aex.Message);
                BtnResetClick(sender, e);
            }

            // form.End();
        }

        private void BtnResetClick(object sender, EventArgs e)
        {
            ImageSketchControl.ChangeImageAndResetFigures(origImageScaled);
        }
    }
}
